package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.DishFlavorsMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分页查询
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        dishPageQueryDTO.check();
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> dishes = (Page<DishVO>) dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(dishes.getTotal(),dishes.getResult());
    }

    /**
     * 新增菜品
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDish(DishDTO dishDTO) {
        // 保存菜品信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        Long dishId = dish.getId();
        // 保存口味信息
        List<DishFlavor> flavorsList = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(flavorsList)){
            flavorsList.forEach(flavors->flavors.setDishId(dishId));
            dishFlavorsMapper.insert(flavorsList);
        }
    }

    /**
     * 根据id查询菜品
     */
    @Transactional
    @Override
    public DishVO queryById(Long dishId) {
        DishVO dishVO = dishMapper.queryById(dishId);
        List<DishFlavor> flavorList = dishFlavorsMapper.queryById(dishId);
        dishVO.setFlavors(flavorList);
        return dishVO;
    }

    /**
     * 删除，批量删除
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return;
        }
        List<Dish> dishes = dishMapper.queryOnSaleDish(ids);
        if (!CollectionUtils.isEmpty(dishes)){
            throw new BaseException(MessageConstant.DISH_ON_SALE);
        }
        dishMapper.deleteByIds(ids);
        dishFlavorsMapper.deleteByIds(ids);
    }

    /**
     * 根据分类id查询菜品
     */
    @Override
    public List<DishVO> queryByCategoryId(Long categoryId) {
        return dishMapper.queryByCategoryId(categoryId);
    }


    /**
     * 修改菜品
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        List<DishFlavor> flavorList = dishDTO.getFlavors();
        flavorList.forEach(flavor->flavor.setDishId(dish.getId()));
        if (!CollectionUtils.isEmpty(flavorList)){
            dishFlavorsMapper.deleteByIds(Collections.singletonList(dish.getId()));
            dishFlavorsMapper.insert(flavorList);
        }
    }

    /**
     * 起售停售
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder().id(id).status(status).build();
        dishMapper.update(dish);
    }

    @Override
    public List<DishVO> queryDishWithFlavor(Dish dish) {
        List<DishVO> dishVOList = dishMapper.queryByCategoryId(dish.getCategoryId());
        dishVOList.forEach(dishVO -> dishVO.setFlavors(dishFlavorsMapper.queryById(dishVO.getId())));
        return dishVOList;
    }
}
